import request from '@/utls/require'

// 用户登录请求参数
export interface LoginParams {
  username: string
  password: string
}

// 用户注册请求参数
export interface RegisterParams {
  username: string
  email: string
  password: string
  school: string
  nickname?: string
  phone?: string
  major?: string
  grade?: number
  student_id?: string
}

// 用户信息
export interface UserInfo {
  id: number
  username: string
  email: string
  first_name?: string
  last_name?: string
  nickname?: string
  avatar?: string
  phone?: string
  school?: string
  major?: string
  grade?: number
  student_id?: string
  is_verified?: boolean
  credit_score?: string
  volunteer_hours?: string
  bio?: string
  preferences?: any
  favorite_categories?: any[]
  reading_interests?: any[]
  is_staff?: boolean
  is_active?: boolean
  date_joined?: string
}

// API 响应类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data?: T
}

/**
 * 用户登录
 */
export function login(params: LoginParams) {
  return request<ApiResponse<UserInfo>>({
    url: '/campus/auth/login',
    method: 'POST',
    data: params
  })
}

/**
 * 用户登出
 */
export function logout() {
  return request<ApiResponse>({
    url: '/campus/auth/logout',
    method: 'POST'
  })
}

/**
 * 用户注册
 */
export function register(params: RegisterParams) {
  return request<ApiResponse<UserInfo>>({
    url: '/campus/auth/register',
    method: 'POST',
    data: params
  })
}

/**
 * 获取当前用户信息
 */
export function getUserInfo() {
  return request<ApiResponse<UserInfo>>({
    url: '/campus/auth/info',
    method: 'GET'
  })
}

/**
 * 更新用户信息
 */
export function updateUserInfo(data: Partial<UserInfo>) {
  return request<ApiResponse>({
    url: '/campus/auth/update',
    method: 'PUT',
    data
  })
}

/**
 * 修改密码
 */
export function changePassword(oldPassword: string, newPassword: string) {
  return request<ApiResponse>({
    url: '/campus/auth/change-password',
    method: 'POST',
    data: {
      old_password: oldPassword,
      new_password: newPassword
    }
  })
}